package com.qujia.label.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.qujia.common.constant.UserConstants;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.exception.CustomException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.StringUtils;
import com.qujia.common.utils.poi.ExcelUtil;
import com.qujia.label.domain.LabelRule;
import com.qujia.label.domain.TimingLabel;
import com.qujia.label.service.ILabelRuleService;
import com.qujia.label.service.ITimingLabelService;
import com.qujia.quartz.util.ScheduleUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qujia.common.annotation.Log;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.enums.BusinessType;
import com.qujia.common.core.page.TableDataInfo;

/**
 * 定时修改标签信息，记录Controller
 * 
 * @author qujia
 * @date 2022-11-16
 */
@RestController
@RequestMapping("/customer/label")
@Api("定时修改标签信息，记录Controller")
public class TimingLabelController extends BaseController
{
    @Autowired
    private ITimingLabelService timingLabelService;
    @Autowired
    private ILabelRuleService labelRuleService;

    /**
     * 查询定时修改标签信息，记录列表
     */
    @GetMapping("/list")
    @ApiOperation("删除标签规则明细")
    public TableDataInfo list(TimingLabel timingLabel)
    {
        LoginUser current= SecurityUtils.getLoginUser();
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysUser user = currentUser.getUser();
        if ("7".equals(user.getUserLevel())){
            List<Long> enterpriseIds =   labelRuleService.selectEnterPriseIdList(user.getUserId());
            if (enterpriseIds != null && enterpriseIds.size()>0) {

                timingLabel.setEnterpriseIdListForAuthor(enterpriseIds);

            } else {
                List<LabelRule> list = new ArrayList<>();
                return getDataTable(list);
            }
        }else{
            timingLabel.setEnterpriseId(current.getUser().getEnterpriseId());
        }


        startPage();
        List<TimingLabel> list = timingLabelService.selectTimingLabelList(timingLabel);
        return getDataTable(list);
    }

    /**
     * 导出定时修改标签信息，记录列表
     */
    @Log(title = "定时修改标签信息，记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("删除标签规则明细")
    public AjaxResult export(TimingLabel timingLabel)
    {
        List<TimingLabel> list = timingLabelService.selectTimingLabelList(timingLabel);
        ExcelUtil<TimingLabel> util = new ExcelUtil<TimingLabel>(TimingLabel.class);
        return util.exportExcel(list, "label");
    }

    /**
     * 获取定时修改标签信息，记录详细信息
     */
    @GetMapping(value = "/{timeId}")
    @ApiOperation("删除标签规则明细")
    public AjaxResult getInfo(@PathVariable("timeId") Long timeId)
    {
        TimingLabel timingLabel=timingLabelService.selectTimingLabelById(timeId);
        if(timingLabel!=null&&timingLabel.getDays()!=null){
            timingLabel.setDaylist(timingLabel.getDays().split(","));
        }
        return AjaxResult.success(timingLabel);
    }

    /**
     * 新增定时修改标签信息，记录
     */
    @Log(title = "定时修改标签信息，记录", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("删除标签规则明细")
    public AjaxResult add(@RequestBody TimingLabel timingLabel)
    {
        LoginUser loginUser=SecurityUtils.getLoginUser();
        timingLabel.setEnterpriseId(loginUser.getUser().getEnterpriseId());
        timingLabel.setCreateDate(new Date());
        if(timingLabel.getDaylist()!=null){
            timingLabel.setDays( StringUtils.join(timingLabel.getDaylist(),","));
        }
        return toAjax(timingLabelService.insertTimingLabel(timingLabel));
    }

    /**
     * 修改定时修改标签信息，记录
     */
    @Log(title = "定时修改标签信息，记录", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("删除标签规则明细")
    public AjaxResult edit(@RequestBody TimingLabel timingLabel)
    {
        timingLabel.setUpdateDate(new Date());
        if(timingLabel.getDaylist()!=null){
            timingLabel.setDays( StringUtils.join(timingLabel.getDaylist(),","));
        }
        return toAjax(timingLabelService.updateTimingLabel(timingLabel));
    }

    /**
     * 删除定时修改标签信息，记录
     */
    @Log(title = "定时修改标签信息，记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{timeIds}")
    @ApiOperation("删除标签规则明细")
    public AjaxResult remove(@PathVariable Long[] timeIds)
    {
        TimingLabel timingLabel=new TimingLabel();
        timingLabel.setDelFlag("1");
        return toAjax(timingLabelService.deleteTimingLabelByIds(timeIds));
    }
}
